package com.tian.config;

import com.tian.constant.RabbitMQConstant;
import com.tian.constant.RabbitMqQueueConstant;
import com.tian.enums.RabbitMQConstantEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

/**
 * {@code @description:} 创建direct类型的交换机
 *
 * @author tianwc 公众号：Java后端技术全栈
 * 在线刷题 1200+java面试题和1000+篇技术文章：<a href="https://woaijava.cc/">博客地址</a>
 * {@code @date:} 2024-03-24 10:20
 * {@code @version:} 1.0
 */
@Slf4j
@Scope("prototype")
@Configuration
public class RabbitMqConfig {
    //用户积分
    @Bean
    public Queue queueMessageUserPoint() {
        return new Queue(RabbitMQConstantEnum.USER_POINT.getQueue(),true);
    }

    @Bean
    TopicExchange exchangeUserPoint() {
        return new TopicExchange(RabbitMQConstantEnum.USER_POINT.getExchange(), true, false);
    }

    @Bean
    Binding bindingExchangeMessageUserPoint(Queue queueMessageUserPoint, TopicExchange exchangeUserPoint) {
        return BindingBuilder.bind(queueMessageUserPoint).to(exchangeUserPoint).with(RabbitMQConstantEnum.USER_POINT.getRoutingKey());
    }

    //发送站内信
    @Bean
    public Queue queueMessageSystemMessage() {
        return new Queue(RabbitMQConstant.SYS_MESSAGE_QUEUE,true);
    }

    @Bean
    TopicExchange exchangeSystemMessage() {
        return new TopicExchange(RabbitMQConstant.SYS_MESSAGE_EXCHANGE, true, false);
    }

    @Bean
    Binding bindingExchangeMessageSystemMessage(Queue queueMessageSystemMessage, TopicExchange exchangeSystemMessage) {
        return BindingBuilder.bind(queueMessageSystemMessage).to(exchangeSystemMessage).with(RabbitMQConstant.SYS_MESSAGE_ROUTING_KEY);
    }

    //发送邮件
    @Bean
    public Queue queueMessageSendEmail() {
        return new Queue(RabbitMQConstant.SEND_EMAIL_QUEUE,true);
    }

    @Bean
    TopicExchange exchangeSendEmail() {
        return new TopicExchange(RabbitMQConstant.SEND_EMAIL_EXCHANGE, true, false);
    }

    @Bean
    Binding bindingExchangeMessageSendEmail(Queue queueMessageSendEmail, TopicExchange exchangeSendEmail) {
        return BindingBuilder.bind(queueMessageSendEmail).to(exchangeSendEmail).with(RabbitMQConstant.SEND_EMAIL_ROUTING_KEY);
    }

    @Bean
    public Queue queueUserUseCoupon() {
        return new Queue(RabbitMQConstant.USER_USE_COUPON_QUEUE, true);
    }

    @Bean
    TopicExchange exchangeUserUseCoupon() {
        return new TopicExchange(RabbitMQConstant.USER_USE_COUPON_EXCHANGE, true, false);
    }

    @Bean
    Binding bindingUserUsingCoupon(Queue queueUserUseCoupon, TopicExchange exchangeUserUseCoupon) {
        return BindingBuilder.bind(queueUserUseCoupon).to(exchangeUserUseCoupon).with(RabbitMQConstant.USER_USE_COUPON_ROUTING_KEY);
    }
    @Bean
    public Queue queueCouponUseStatistics() {
        return new Queue(RabbitMQConstant.COUPON_USE_STATISTICS_QUEUE, true);
    }

    @Bean
    TopicExchange exchangeCouponUseStatistics() {
        return new TopicExchange(RabbitMQConstant.COUPON_USE_STATISTICS_EXCHANGE, true, false);
    }

    @Bean
    Binding bindingCouponUseStatistics(Queue queueCouponUseStatistics, TopicExchange exchangeCouponUseStatistics) {
        return BindingBuilder.bind(queueCouponUseStatistics).to(exchangeCouponUseStatistics).with(RabbitMQConstant.COUPON_USE_STATISTICS_ROUTING_KEY);
    }
    @Scope("prototype")
    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate();
        rabbitTemplate.setConnectionFactory(connectionFactory);
        //设置消息投递失败的策略，有两种策略：自动删除或返回到客户端。
        //我们既然要做可靠性，当然是设置为返回到客户端(true是返回客户端，false是自动删除)
        rabbitTemplate.setMandatory(true);
        return rabbitTemplate;
    }
}

